home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj0593.zip / 1105056A < prev    next >
Text File  |  1993-03-09  |  2KB  |  113 lines

  1. // main.cpp  - Frog Pond Listing 2
  2. #include <frog.h>
  3.  
  4. main()
  5.     {
  6.     PMF_VV pf = &Creature::move;  // bound to a virtual
  7.     Pond walden;
  8.     Creature *p1 = new Creature(Creature::TADPOLE,"Wiggly");
  9.     walden.insert(p1);
  10.     Creature *p2 = new Creature(Creature::BULLFROG,"Croaker");
  11.     walden.insert(p2);
  12.  
  13.     walden.activate( pf );  // Tell all creatures to move
  14.  
  15.     return 0;
  16.     }
  17.  
  18. /---------------------------------------------------------
  19. //  Frog.h
  20. #ifndef FROG_H
  21. #define FROG_H
  22. #include <iostream.h>
  23. #include <rw/gslist.h>
  24.  
  25. class Creature
  26.     {
  27. public:
  28.     enum type{BULLFROG,TADPOLE};
  29.     Creature(type,char *);
  30.     virtual void move() { object->move(); }
  31. protected:
  32.     Creature() : object(0) {}// default invoked by derived
  33. private:
  34.     Creature *object;
  35.     };
  36.  
  37. declare(GSlist,Creature)
  38.  
  39. class Frog : public Creature
  40.     {
  41. protected:
  42.     char *name;
  43. public:
  44.     Frog(char *p="Noname Frog") : name(p) {}
  45.     ~Frog(); 
  46.     char *Name() { return name; }
  47.     virtual void move() {} 
  48.     };
  49.  
  50. class Tadpole : public Frog
  51.     {
  52. public:
  53.     Tadpole(char *name) : Frog(name) {};
  54.     void move(){cout << name << " Swimming jerkily\n";}
  55.     };
  56.  
  57. class Bullfrog : public Frog
  58.     {
  59. public:
  60.     Bullfrog(char *name) : Frog(name) {};
  61.     void move() { cout << name << " - I'M JUMPING\n"; }
  62.     };
  63.  
  64. class Pond
  65.     {
  66.     GSlist(Creature) list;  // singly linked creature list
  67. public:
  68.     void activate( void (Creature::*pf) () );
  69.     void insert(Creature*);
  70.     };
  71.  
  72. // this typedef must be after the class declarations
  73. // it may be useful to simplify declarations in the users program.
  74. // useful for pointing to move(void) 
  75. typedef void (Creature::*PMF_VV)();   
  76. #endif
  77. //------------------------------------------------------------
  78. // Frog.cpp
  79. #include <stdlib.h>
  80. #include <frog.h>
  81.  
  82. void Pond::insert(Creature *f)
  83.     {
  84.     list.insert(f);
  85.     }
  86.  
  87. void Pond::activate( PMF_VV pmf_vv )
  88.     {
  89.     GSlistIterator(Creature) it(list);
  90.     Creature *resident;
  91.     while ( resident = it() )
  92.     {
  93.     (resident->*pmf_vv)();
  94.     }
  95.     }
  96.  
  97. Creature::Creature(Creature::type t,char *name)
  98.     {
  99.     if ( t == TADPOLE )
  100.     object = new Tadpole(name);
  101.     else if( t == BULLFROG)
  102.     object = new Bullfrog(name);
  103.     else
  104.     {
  105.     cerr << "Invalid type in Creature ctor/n";
  106.     exit(2);
  107.     }
  108.     }
  109.  
  110. Frog::~Frog() {}
  111.  
  112.  
  113.